import React from 'react';
import { schema } from '@/ts/base';
import FullScreenModal from '@/components/Common/fullScreen';
import { Button, Form as FormAntd, InputNumber, Radio, message } from 'antd';
interface Iprops {
  things: schema.XThing[];
  finished: () => void;
}

let printForm = {
  RFIDErweima: {
    dXPos: 0,
    dYPos: 10,
    QrSize: 25,
  },
  RFIDText: {
    dXPos: 26,
    dYPos: 10,
    size: 14,
    unitXPos: 15,
    unitYPos: 31,
  },
};
let dsChoose: string = 'yes';
export const DasPrint: React.FC<Iprops> = ({ things, finished }) => {
  // 得实打印
  // 启动打印服务
  const getDSInitService = () => {
    if (dsChoose === 'no') {
      message.warn('请联系对接方，获取相应文件');
    } else {
      window.DSInitService();
      window.web_Wait(1000, () => {
        test1(things);
      });
    }
  };
  // 查询打印机编码
  const test1 = (selectionList) => {
    window.web_DSEnumPrinter(callBackFun);
    function callBackFun(retVal) {
      if (retVal.RtnVal == null) return;
      if (retVal.RtnCode == 0) {
        for (let i in retVal.RtnVal) {
          var ii = i + 1;
          var index = retVal.RtnVal[i].indexOf('@');
          port = retVal.RtnVal[i].substring(index + 1);
          break;
        }
        test2(selectionList);
      } else {
        console.log('未连接打印机');
      }
    }
  };
  // 打开打印机
  const test2 = (selectionList) => {
    var printerName = port;
    window.web_DSOpenPrinter(printerName, callBackFun);

    function callBackFun(retVal) {
      console.log('打开打印机：' + retVal.RtnVal);
      GetPrintStatus(selectionList);
    }
  };
  // 获取打印机状态
  const GetPrintStatus = (selectionList) => {
    window.web_DSGetStarPrint(callBackFun);

    function callBackFun(retVal) {
      printData(selectionList);
    }
  };
  // 写字符EPC
  const ZFEPC = async (code) => {
    let ncode = BigInt(code).toString(16).toUpperCase().padStart(24, '0');
    window.web_DAS_RFIDTag_Write_EPCIDA(ncode, callBackFun);
    function callBackFun(retVal) {
      if (retVal.RtnVal == 1) {
        writeFailed = false;
        console.log('EPC写入成功,点击打印');
      } else {
        writeFailed = true;
        console.log('EPC写入失败');
      }
    }
  };
  const printData = async (selectionList) => {
    for (let i = 0; i < selectionList.length; i++) {
      for (let x = 0; x < 1; x++) {
        addNumber = 0;
        await ZFEPC(selectionList[i].id);
        window.web_DSPrintQrCodeForLibrary(
          2,
          30,
          printForm.RFIDErweima.dXPos,
          printForm.RFIDErweima.dYPos,
          printForm.RFIDErweima.QrSize,
          2,
          false,
          15,
          selectionList[i].id,
        );
        window.web_DSPrintQrCodeForLibrary(
          0,
          30,
          printForm.RFIDErweima.dXPos,
          printForm.RFIDErweima.dYPos,
          printForm.RFIDErweima.QrSize,
          2,
          false,
          15,
          selectionList[i].id,
        );
        window.web_DSZPLSetFont('宋体');
        let len: number = selectionList[i].belongId.length;
        if (len < 6) {
          window.web_DSZPLPrintText(
            0,
            30,
            printForm.RFIDText.unitXPos,
            printForm.RFIDText.unitYPos,
            false,
            printForm.RFIDText.size,
            selectionList[i].belongId,
          );
        } else {
          for (let z = 0; z < Math.ceil((len / 6).toFixed(1) as any as number); z++) {
            let name = selectionList[i].belongId.substring(0 + z * 6, 6 + z * 6);
            window.web_DSZPLPrintText(
              0,
              30,
              printForm.RFIDErweima.dXPos,
              printForm.RFIDErweima.dYPos + printForm.RFIDErweima.QrSize + z * 5,
              false,
              printForm.RFIDText.size,
              name,
            );
          }
        }
        if (writeFailed) {
          window.web_DSZPLPrintText(0, 6, 26, 10, false, 14, '打印错误,数据写入失败');
        } else {
          for (let j = 0; j < configData.length; j++) {
            let itemKey = configData[j].itemKey;
            let itemName = configData[j].itemName;
            let value = selectionList[i][configData[j].itemKey];
            // 根据不同的 itemKey 处理打印内容
            window.web_DSZPLPrintText(
              0,
              30,
              printForm.RFIDText.dXPos,
              printForm.RFIDText.dYPos + j * 5,
              false,
              printForm.RFIDText.size,
              itemName + ':' + value,
            );
          }
          window.web_DSPrintQrCodeForLibrary(
            3,
            30,
            printForm.RFIDErweima.dXPos,
            printForm.RFIDErweima.dYPos,
            printForm.RFIDErweima.QrSize,
            2,
            false,
            15,
            selectionList[i].id,
          );
        }
      }
    }
  };

  return (
    <FullScreenModal
      open
      title={'标签打印'}
      width={'360px'}
      bodyHeight={'360px'}
      footer={
        <Button
          type="primary"
          onClick={() => {
            getDSInitService();
            finished();
          }}>
          打印
        </Button>
      }
      onCancel={() => finished()}>
      <div>
        <Radio.Group
          defaultValue="yes"
          onChange={(e) => {
            dsChoose = e.target.value;
          }}>
          <Radio value="yes">已安装得实打印助手并启动</Radio>
          <Radio value="no">未安装得实打印助手并启动</Radio>
        </Radio.Group>
        <FormAntd layout="vertical">
          <FormAntd.Item label="二维码横坐标">
            <InputNumber
              defaultValue={0}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDErweima.dXPos = value;
                }
              }}
            />
          </FormAntd.Item>
          <FormAntd.Item label="二维码纵坐标">
            <InputNumber
              defaultValue={10}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDErweima.dYPos = value;
                }
              }}
            />
          </FormAntd.Item>
          <FormAntd.Item label="二维码大小">
            <InputNumber
              defaultValue={25}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDErweima.QrSize = value;
                }
              }}
            />
          </FormAntd.Item>
          <FormAntd.Item label="文本横坐标">
            <InputNumber
              defaultValue={26}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDText.dXPos = value;
                }
              }}
            />
          </FormAntd.Item>
          <FormAntd.Item label="文本纵坐标">
            <InputNumber
              defaultValue={10}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDText.dYPos = value;
                }
              }}
            />
          </FormAntd.Item>
          <FormAntd.Item label="文字大小">
            <InputNumber
              defaultValue={14}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDText.size = value;
                }
              }}
            />
          </FormAntd.Item>
          <FormAntd.Item label="单位名称横坐标">
            <InputNumber
              defaultValue={15}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDText.unitXPos = value;
                }
              }}
            />
          </FormAntd.Item>
          <FormAntd.Item label="单位名称纵坐标">
            <InputNumber
              defaultValue={31}
              onChange={(e) => {
                const value = e;
                if (value) {
                  printForm.RFIDText.unitYPos = value;
                }
              }}
            />
          </FormAntd.Item>
        </FormAntd>
      </div>
    </FullScreenModal>
  );
};
